/*
 * Copyright (c) 2016 . All Rights Reserved.
 */
package com.cgmnx.admin.web.controller;

import com.cgmnx.admin.biz.ReplyBiz;
import com.cgmnx.admin.common.bean.ApiRes;
import com.cgmnx.admin.common.bean.Limiter;
import com.cgmnx.admin.dao.condition.ReplyCondition;
import com.cgmnx.admin.dao.entity.ReplyEntity;
import com.cgmnx.admin.model.enums.ReplyAuditStatus;
import com.cgmnx.admin.web.converter.entity.ReplyConditionConverter;
import com.cgmnx.admin.web.converter.entity.ReplyEntryConverter;
import com.cgmnx.admin.web.converter.vm.ReplyVmConverter;
import com.cgmnx.admin.web.model.param.ReplyQueryParam;
import com.cgmnx.admin.web.model.param.ReplySaveParam;
import com.cgmnx.admin.web.model.vm.ReplyVm;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author: created  Date: 16/12/12 Time: 下午3:02
 * @version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/cgmnx/reply/")
public class ReplyController {

    @Resource
    private ReplyBiz replyBiz;

    @Resource
    private ReplyConditionConverter conditionConverter;
    @Resource private ReplyEntryConverter replyEntryConverter;

    @Resource
    private ReplyVmConverter replyVmConverter;

    @RequestMapping(value = "/queryPage.json")
    public ApiRes queryPage(@RequestBody(required = false) ReplyQueryParam param) {

        final ReplyCondition condition = conditionConverter.convert(param);

        final int count = replyBiz.count(condition);
        if(count < 1) {
            return ApiRes.emptyList();
        }
        final List<ReplyEntity> replyEntities = replyBiz.queryPage(condition,Limiter.create(param.getPageNum(),param.getPageSize()));
        if(CollectionUtils.isEmpty(replyEntities)) {
            return ApiRes.emptyList();
        }

        final List<ReplyVm> collect = replyEntities.stream().map(replyEntity -> replyVmConverter.convert(replyEntity)).collect(Collectors.toList());
        return ApiRes.list(collect,count);
    }
    @RequestMapping("/save.json")
    public ApiRes save(@RequestBody ReplySaveParam item) {

        ReplyEntity entity = replyEntryConverter.convert(item);

        replyBiz.save(entity);

        return ApiRes.emptyList();
    }

    @RequestMapping(value = "/queryReplyAuditStatus.json")
    public ApiRes queryReplyAuditStatus() {
        return ApiRes.returnSuccess(getReplyAuditStatusMap());
    }
    private List<Map<String, Object>> getReplyAuditStatusMap() {

        List<Map<String, Object>> list = Lists.newArrayList();
        for(ReplyAuditStatus value : ReplyAuditStatus.values()) {
            Map<String,Object> map = Maps.newHashMap();
            map.put("code",value.getCode());
            map.put("desc",value.getDesc());
            list.add(map);
        }
        return list;
    }

    @RequestMapping("/queryStatus.json")
    public ApiRes queryStatus(@RequestParam("id") Integer id, @RequestParam("status") Integer status) {
        if(Objects.isNull(id) || Objects.isNull(status)) {
            return ApiRes.returnFail("笔记id和状态不能为空");
        }
        final ReplyEntity replyEntity = replyBiz.queryById(id);

        if(Objects.isNull(replyEntity)) {
            return ApiRes.returnFail("笔记不存在");
        }

        replyBiz.audit(id, status, replyEntity.getStatus().getCode());

        return ApiRes.returnSuccess("审核成功");
    }
}